<template>
  <div class="dialog">
    <el-dialog :title="'休药期配置' + titleText" :visible.sync="showDialog" width="900" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="养殖品种:" prop="fishVarietyId" class="w50p">
          <el-select filterable clearable v-model="formData.fishVarietyId" placeholder="请选择" filterable clearable>
            <el-option v-for="(item, index) in varietyList" :label="item.varietyName" :value="item.fishVarietyId"
              :key="index + 'fishVariety'" />
          </el-select>
        </el-form-item>
        <el-form-item label="药品名称:" prop="materialId" class="w50p">
          <el-select filterable clearable v-model="formData.materialId" filterable clearable @change="drugChange">
            <el-option v-for="item of materialList" :key="item.materialId + 'materialId'" :label="item.name"
              :value="item.materialId" />
          </el-select>
        </el-form-item>
        <el-form-item label="用药标准:" prop="amount" class="w50p">
          <el-input v-model="formData.amount" type="number" min="0" clearable placeholder="请输入用药数量">
            <i slot="suffix" style="color:#000;">{{ materialUnit ? materialUnit + '/' : '' }}</i>
            <el-select filterable clearable v-model="formData.areaUnit" filterable allow-create slot="append"
              style="width: 100px;height: 90%;">
              <el-option v-for="item of areaUnit" :key="item.name + 'areaUnit'" :label="item.value" :value="item.name" />
            </el-select>
          </el-input>
        </el-form-item>
        <el-form-item label="休药期:" prop="days" class="w50p">
          <el-input v-model="formData.days" type="number" clearable placeholder="请输入休药期天数">
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DrugAlarmConfigApi from "@/api/health-management/drug-alarm-config";
import MaterialApi from "@/api/basic-info-manage/material";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety";
import EnumApi from "@/api/enum";
import { dialogMixin } from "@/utils/dialogMixin"
export default {
  name: '',
  components: {},
  mixins: [dialogMixin],
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
  },
  data () {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0 && value > 0) {
          callback();
        } else {
          callback(new Error('请输入大于0的数'));
        }
      } else {
        callback(new Error(rule.emptyMsg));
      }
    };
    let positiveNum = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value > 0) {
          callback();
        } else {
          callback(new Error('请输入大于0的数'));
        }
      } else {
        callback(new Error(rule.emptyMsg));
      }
    };
    return {
      formData: {},
      rules: {
        days: [
          { required: true, emptyMsg: "请输入天数", trigger: ["change", "blur"], validator: checkInt }
        ],
        amount: [
          { required: true, emptyMsg: "请输入数量", trigger: ["change", "blur"], validator: positiveNum }
        ],
        materialId: [
          { required: true, message: "请选择物料", trigger: ["change", "blur"] }
        ],
        fishVarietyId: [
          { required: true, message: "请选择品种", trigger: ["change", "blur"] }
        ],
      },
      materialUnit: undefined,
      materialList: [],
      staffList: [],
      areaUnit: [],
      varietyList: [],
      showDialog: this.show
    };
  },
  watch: {
    show (v) {
      this.showDialog = v;
    },
    nowChanceStau (v) {
      if (v === 'add') {
        this.formData = {
          areaUnit: this.areaUnit[0].name
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
      }
    }
  },
  created () {
  },
  mounted () {
    this.queryMaterial()
    this.queryEnum()
    this.queryFishVariety()
  },
  methods: {
    drugChange (e) {
      this.materialList.find((item) => {
        if (item.materialId === e) {
          console.log(item);
          this.materialUnit = item.auxiliaryUnitNo
          // this.formData.auxiliaryUnitNo = item.auxiliaryUnitNo
        }
      })
    },
    queryEnum () {
      EnumApi.list({ enumType: 'AreaUnitType' }).then(res => {
        this.areaUnit = res.data
      })
    },
    queryMaterial () {
      MaterialApi.queryByCondition({ materialTypeNo: 'drug' }).then(res => {
        this.materialList = res.data;
      })
    },
    queryFishVariety () {
      FishVarietyApi.queryByCondition().then(res => {
        this.varietyList = res.data;
      })
    },
    save (formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            DrugAlarmConfigApi.edit(this.formData).then(res => {
              if (res.success) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error("系统错误,请稍后再试");
              }
            });
          } else if (this.nowChanceStau === "add") {
            DrugAlarmConfigApi.add(this.formData).then(res => {
              if (res.success) {
                this.$message.success("添加成功");
                this.close()
              } else if (res.success === false && res.errCode === 200) {
                this.$message.error(res.message);
              } else this.$message.error("系统错误,请稍后再试");
            });
          }
        } else {
          return false;
        }
      });
    },
    close () {
      this.$emit("closeDialog");
    },
    cancel () {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
